package com.xiaoxie.boot.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.xiaoxie.boot.bean.UserPhoto;
import com.xiaoxie.boot.service.UserPhotoService;
import com.xiaoxie.boot.utils.DataInfo;
import com.xiaoxie.boot.utils.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * @author xiaoxie
 * @create 2022-02-15 19:44
 */
@Api(tags = "用户头像API")
@RestController
@Slf4j
@RequestMapping("/photo")
public class UserPhotoController {

    @Autowired
    private UserPhotoService userPhotoService;

    /**
     * 上传头像
     *
     * @param request
     * @param multipartFile
     * @return
     * @throws IOException
     */
    @PostMapping("/upload")
    @ApiOperation(value = "上传头像", notes = "上传头像")
    @ApiOperationSupport(author = "小谢")
    public DataInfo upload(HttpServletRequest request, @RequestParam("file") MultipartFile multipartFile) throws IOException {
        HttpSession session = request.getSession();
        Integer id = (Integer) session.getAttribute("id");
        String name = (String) session.getAttribute("name");

        //获取上传的图片的文件名
        String fileName = multipartFile.getOriginalFilename();
        log.info("上传的头像名称为：" + fileName);

        //获取上传文件的大小
        long size = multipartFile.getSize();
        log.info("上传的头像大小为：" + size);

        //获取上传文件的后缀名
        assert fileName != null;
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        log.info("上传的头像后缀为：" + suffixName);

        //将UUID作为文件名，并去掉四个"-"
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");

        //将uuid和后缀名拼接后的结果作为最终的文件名
        fileName = uuid + suffixName;
        log.info("上传的头像的新的名称为：" + fileName);

        //获取项目images文件夹的地址
        String url = request.getSession().getServletContext().getRealPath("images");
        log.info("获取项目classes/static/images的地址为：" + url);

        //图片存储目录及图片名称
        String imagePath = "images" + File.separator + fileName;
        log.info("图片存储目录及图片名称为：" + imagePath);

        File file = new File(url);
        if (!file.exists()) {
            //若不存在，则创建目录
            file.mkdir();
        }

        //图片保存路径
        String savePath = url + File.separator + fileName;
        log.info("图片保存路径为：" + savePath);

        //保存图片
        multipartFile.transferTo(new File(savePath));
        log.info(name + "试图保存头像到数据库");
        int i = userPhotoService.addPhoto(new UserPhoto(null, id, imagePath, DateUtil.getTimestamp()));
        if (i == 1) {
            log.info(name + "保存头像到数据库成功");
            return DataInfo.ok();
        } else {
            log.info(name + "保存头像到数据库失败");
            return DataInfo.fail("上传失败");
        }
    }

    /**
     * 查询头像
     *
     * @param request
     * @return
     */
    @GetMapping("/queryPhoto")
    @ApiOperation(value = "查询头像", notes = "查询头像")
    @ApiOperationSupport(author = "小谢")
    public DataInfo queryPhoto(HttpServletRequest request) {
        HttpSession session = request.getSession();
        Integer id = (Integer) session.getAttribute("id");
        UserPhoto userPhoto = userPhotoService.selectPhoto(id);
        return DataInfo.ok("成功", userPhoto);
    }

}
